<script setup lang="ts">
import { fetchRecordApi } from "@/api/record";
import { getCurrentInstance, ref, onMounted } from "vue";
import { useStore } from "@/store";
import { useRouter } from "vue-router";

const store = useStore();
const router = useRouter();

// 历史足迹数据
let recordData = ref();

const { proxy }: any = getCurrentInstance();
const loading = ref(true);
// 查询历史足迹
function fetchRecord() {
  fetchRecordApi().then((res) => {
    let { success, message, result } = res;
    loading.value = false;
    if (success) {
      recordData.value = result;
    } else {
      proxy.$message.error(message);
    }
  });
}

onMounted(() => {
  if (store.state.userStatus) {
    fetchRecord();
  } else {
    const { pathname } = window.location;
    router.replace({
      path: "/login",
      query: {
        redirect: pathname,
      },
    });
  }
});

if (store.state.userStatus) {
  fetchRecord();
} else {
  const { pathname } = window.location;
  router.replace({
    path: "/login",
    query: {
      redirect: pathname,
    },
  });
}

function toDetailByClick(item: any) {
  // 拿到房屋的id
  const { recordId: id } = item;
  router.push({ path: `/roomDetail/${id}` });
  store.commit("setRoomId", id);
}
</script>
 
<template>
  <div class="record">
    <div class="main-wrapper">
      <el-skeleton :loading="loading">
        <template #template>
          <div class="column">
            <div class="item" v-for="i in 9" :key="i">
              <el-skeleton-item
                variant="image"
                style="width: 315px; height: 240px; border-radius: 4px"
              />
              <el-skeleton-item
                variant="p"
                style="width: 100%; margin-top: 15px"
              />
              <el-skeleton-item variant="p" style="width: 30%" />
            </div>
          </div>
        </template>
        <template #default>
          <div class="column" v-if="recordData.length > 0">
            <div
              class="item"
              v-for="(item, index) in recordData"
              @click="toDetailByClick(item)"
            >
              <el-image :src="item.pictureUrl" :alt="item.title" />
              <p class="title">{{ item.title }}</p>
              <p class="price">￥{{ item.price }}</p>
            </div>
          </div>
          <el-empty v-else description="暂无浏览器记录" />
        </template>
      </el-skeleton>
    </div>
  </div>
</template>

<style lang="scss" >
.record {
  .main-wrapper {
    @include main-wrapper(30px);
    .column {
      column-count: 3;
      .item {
        width: 315px;
        overflow: hidden;
        margin-bottom: 25px;
        cursor: pointer;
        text-align: left;
        display: inline-block;
        img {
          width: 315px;
          height: auto;
          border-radius: 4px;
          .title {
            width: 315px;
            font-size: 18px;
            margin: 15px 0;
            font-weight: bold;
          }
          .price {
            font-size: 16px;
          }
        }
      }
    }
  }
}
</style>